package cate.game.role.farm.guildBoss;

import cate.common.table.d.GDMail;
import cate.common.table.d.GDObj;
import cate.common.table.d.GDOperation;
import cate.common.table.guild.guildboss.GuildBossCombatRow;
import cate.game.mail.po.MailBuilder;
import cate.game.play.proce.play.PlayPO;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.farm.guildBoss.msg.GuildBossChallengeResultResp;
import cate.game.role.farm.guildBoss.msg.MyGuildBossDataResp;
import cate.game.role.mail.Mail;
import cate.game.role.res.guild.boss.msg.GuildBossDataResp;
import cate.game.role.res.guild.boss.msg.GuildBossRankResp;
import cate.game.role.res.guild.po.GuildPO;

import java.util.ArrayList;
import java.util.List;

public class GuildBossTools {

    private GuildBossTools() {
    }

    /**
     * 从剧本中找出对boss造成得伤害
     */
    public static synchronized long calcDamage(PlayPO play) {
        return play.teamEnd.b.getAllHero().get(0).performance.hurt;
    }


    /**
     * 处理挑战结果
     */
    public static void handleResult(Role role, long damage, GuildBossCombatRow tpl, boolean mop) {
        GuildPO guild = role.getRes().guild.getGuild();
        if (guild == null) {
            return;
        }
        // 给boss扣血
        guild.boss.hurt(damage);

        boolean killed = guild.boss.isKilled();
        // 记录一下伤害排行榜
        guild.boss.rank.record(role, damage);

        GuildBossChallengeResultResp resultResp = new GuildBossChallengeResultResp();
        // 击杀了
        if (killed) {
            // 击杀奖励走邮件
            //添加建设值
//            role.getRes().guild.getGuild().build.addConstructive(tpl.kill_build_exp);
            MixRes reward = new MixRes(tpl.killRewardStr);
            role.getRes().guild.performance.addPValue((int) reward.count(GDObj.Type.NUM, GDObj.Num.P));
            role.getHistory().action.killGuildBoss();

            List<Object> params = new ArrayList<>();
            params.add(tpl.combatNum);
            Mail mail =
                    new MailBuilder()
                            .setContentId(GDMail.Tid.GUILD_BOSS_KILL)
                            .setParamList(params)
                            .setReward(reward)
                            .setOperation(GDOperation.GUILD_BOSS_KILL)
                            .setOperationSub(tpl.combatNum)
                            .build();
            role.getMail().add(mail);
//            resultResp.guildExp = tpl.kill_build_exp;

            guild.boss.prepare(role);
            // 通知在线的成员
            guild.member
                    .getOnlineRole()
                    .forEach(
                            a -> {
                                a.sendNow(new GuildBossDataResp(a.getRes().guild.getGuild().boss));
                                a.getFarm().guildBoss.noticeUpdate();
                            });
        }
        // 伤害奖励直接发
        MixRes reward = new MixRes(tpl.damageRewardStr);
//        double rate = damage / (double) tpl.bossHp;
//        if (rate > 0.1) {
//            reward.multiplyDouble(rate * 10);
//        }
        role.getRes().guild.performance.addPValue((int) reward.count(GDObj.Type.NUM, GDObj.Num.P));

//        GuildBossRewardRow row = role.getGame().table.guildBoss.reward.random(tpl.damage_group);
//        reward.addList(new MixRes(row.rewardStr));
        new MixResAdder().setRes(reward).exe(role);

        resultResp.killed = killed;
        resultResp.reward = reward;
        resultResp.mop = mop;
        resultResp.damage = damage;
        role.sendNow(resultResp);

        role.getFarm().guildBoss.playTimes.added();
        role.sendNow(new MyGuildBossDataResp(role));
        role.sendNow(new GuildBossDataResp(role.getRes().guild.getGuild().boss));
        role.sendNow(new GuildBossRankResp(role.getRes().guild.getGuild().boss.getCurrentCombatNum(),
                role.getRes().guild.getGuild().boss.rank));
    }
}
